// 获取应用实例
var app = getApp();
Page({
    data: {
        userObj: {},
        userPicture: null,
    },
    onLoad () {
        // console.log(app.globalData);
        
    },
    usersObj () {
        let user = wx.getStorageSync('user')
        this.setData({
            userObj : user,
            userPicture: app.globalData.call+'/static/face'+user.picture
        })
    },
    infoUp () {
        wx.navigateTo({
            url: '../information/information'
        })
    },
    onShow(){
        this.login();
        this.usersObj();
    },
    login () {
        let _this = this;
        wx.login({
            complete: (res) => {
                wx.request({
                  method: 'GET',
                  url: app.globalData.call+'/logOn/login',
                  data: {
                      code: res.code,
                  },
                  success(res){
                    //   console.log(res)
                      wx.setStorageSync('user',res.data.page);
                  }
                })
            },
        })
    },
    attendanceRecord(){
        wx.navigateTo({
            url: '../attendance/attendance'
            //实际路径要写全
        })
    },
    faceUp: function () {
        let _this = this;
        wx.chooseImage({
            count: 1,
            sizeType: ['original', 'compressed'],
            sourceType: ['album', 'camera'],
            success: function (res) {
                // tempFilePath可以作为img标签的src属性显示图片
                // res.tempFilePaths[0]
                // console.log(res.tempFilePaths[0]);
                // console.log(wx.getStorageSync('user').openId);
                wx.uploadFile({
                    name: 'file',
                    url: app.globalData.call + '/logOn/send',
                    filePath: res.tempFilePaths[0],
                    header: {
                        "Content-Type": "multipart/form-data",
                        'accept': 'application/json'
                    },
                    formData: {
                        openId: wx.getStorageSync('user').openId
                    },
                    success: function (res) {
                        console.log(res);
                        _this.userInfo(wx.getStorageSync('user').pid)
                    }
                });
            }
        });
    },
    userInfo(pid){
        let _this = this;
        wx.request({
            url: `${app.globalData.call}/personnel/info/${pid}`,
            success(res){
                wx.setStorageSync('user', res.data.personnel);
                _this.usersObj();
            }
        })
    }
});
